<template>
  <ContentWrap>
    <!-- 订单信息 -->
    <el-descriptions title="订单信息">
      <el-descriptions-item label="订单号: ">{{ formData.no }}</el-descriptions-item>
      <el-descriptions-item label="买家: ">
        {{ formData?.user?.nickname }}
        <el-button size="small" type="primary" style="margin-left:16px;" @click="blockUser">
          {{formData?.user?.blocked ? "取消拉黑" : "拉黑"}}
        </el-button>
      </el-descriptions-item>
      <el-descriptions-item label="订单类型: ">
        <dict-tag :type="DICT_TYPE.TRADE_ORDER_TYPE" :value="formData.type!" />
      </el-descriptions-item>
      <el-descriptions-item label="订单来源: ">
        <dict-tag :type="DICT_TYPE.TERMINAL" :value="formData.terminal!" />
      </el-descriptions-item>
      <el-descriptions-item label="买家留言: ">{{ formData.userRemark }}</el-descriptions-item>
      <el-descriptions-item label="商家备注: ">{{ formData.remark }}</el-descriptions-item>
      <el-descriptions-item label="支付单号: ">{{ formData.payOrderId }}</el-descriptions-item>
      <el-descriptions-item label="支付宝渠道号: ">{{ formData.channelOrderNo }}</el-descriptions-item>
      <el-descriptions-item label="付款方式: ">
        <dict-tag :type="DICT_TYPE.PAY_CHANNEL_CODE" :value="formData.payChannelCode!" />
      </el-descriptions-item>
      <el-descriptions-item label="下单地区: ">
        {{formData.orderAreaName}}
      </el-descriptions-item>
      <el-descriptions-item v-if="formData.brokerageUser" label="推广用户: ">
        {{ formData.brokerageUser?.nickname }}
      </el-descriptions-item>
    </el-descriptions>
    <!-- 订单状态 -->
    <el-descriptions :column="1" title="订单状态">
      <el-descriptions-item label="订单状态: ">
        <dict-tag :type="DICT_TYPE.TRADE_ORDER_STATUS" :value="formData.status!" />
      </el-descriptions-item>
      <el-descriptions-item v-hasPermi="['trade:order:update']" label-class-name="no-colon">
        <el-button
          v-if="formData.status! === TradeOrderStatusEnum.UNPAID.status && false"
          type="primary"
          @click="updatePrice"
        >
          调整价格
        </el-button>
        <el-button type="primary" @click="remark">备注</el-button>
        <el-button v-if="formData.status==30" type="primary" @click="sendEmailByHand">手动发送邮件</el-button>
        <el-button v-hasPermi="['trade:order:cardkey-secret']" :loading="isLoadingCardKey" @click="checkCardKey" style="margin-left:16px;" type="primary" v-if="isShowCheckCardKeyBtn">查看日志</el-button>
        <!--40是已取消-->
        <template v-if="formData.manual">
          <el-button
            type="primary"
            @click="openAuditForm"
          >
            填写卡密
          </el-button>
        </template>
        <el-button
          v-if="(formData.auditStatus == 0 || formData.deliveryStatus == 2) && ![40].includes(formData.status)"
          type="danger"
          @click="openDrawBackForm"
        >
          售后退款
        </el-button>

        <!-- 待发货 -->
        <template v-if="formData.status! === TradeOrderStatusEnum.UNDELIVERED.status">
          <!-- 快递发货 -->
          <el-button
            v-if="formData.deliveryType === DeliveryTypeEnum.EXPRESS.type"
            type="primary"
            @click="delivery"
          >
            发货
          </el-button>
          <el-button
            v-if="formData.deliveryType === DeliveryTypeEnum.EXPRESS.type"
            type="primary"
            @click="updateAddress"
          >
            修改地址
          </el-button>
          <!-- 到店自提 -->
          <el-button
            v-if="formData.deliveryType === DeliveryTypeEnum.PICK_UP.type && showPickUp"
            type="primary"
            @click="handlePickUp"
          >
            核销
          </el-button>
        </template>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label><span style="color: red">提醒: </span></template>
        买家付款成功后，货款将直接进入您的商户号（微信、支付宝）<br />
        请及时关注你发出的包裹状态，确保可以配送至买家手中 <br />
        如果买家表示没收到货或货物有问题，请及时联系买家处理，友好协商
      </el-descriptions-item>
    </el-descriptions>

    <!-- 商品信息 -->
    <el-descriptions title="商品信息">
      <el-descriptions-item labelClassName="no-colon">
        <el-row :gutter="20">
          <el-col :span="15">
            <el-table :data="formData.items" border>
              <el-table-column label="商品" prop="spuName" width="auto">
                <template #default="{ row }">
                  {{ row.spuName }}
                  <el-tag v-for="property in row.properties" :key="property.propertyId">
                    {{ property.propertyName }}: {{ property.valueName }}
                  </el-tag>
                  <p >
                    <el-tag>
                      面值：{{row.denomination}}
                    </el-tag>
                  </p>
                </template>
              </el-table-column>
              <el-table-column label="商品原价" prop="price" width="150">
                <template #default="{ row }">{{ row.itemNominalValue / 100 || 0 }} {{ row.currencyCode }}</template>
              </el-table-column>
              <el-table-column label="数量" prop="count" width="100" />
              <el-table-column label="合计" prop="payPrice" width="150">
                <template #default="{  }">{{ formData.paymentPrice / 100 }} {{ formData.paymentCurrency }}</template>
              </el-table-column>
              <el-table-column label="售后状态" prop="afterSaleStatus" width="120">
                <template #default="{ row }">
                  <dict-tag
                    :type="DICT_TYPE.TRADE_ORDER_ITEM_AFTER_SALE_STATUS"
                    :value="row.afterSaleStatus"
                  />
                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="10" />
        </el-row>
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions :column="4">
      <!-- 第一层 -->
      <el-descriptions-item label="商品总额: ">
        {{ (formData.itemNominalValue  / 100)! }}  {{ formData.orderCurrency }}
      </el-descriptions-item>
<!--      <el-descriptions-item label="运费金额: ">-->
<!--        {{ fenToYuan(formData.deliveryPrice!) }} 元-->
<!--      </el-descriptions-item>-->
<!--      <el-descriptions-item label="订单调价: ">-->
<!--        {{ fenToYuan(formData.adjustPrice!) }} 元-->
<!--      </el-descriptions-item>-->
<!--      <el-descriptions-item v-for="item in 1" :key="item" label-class-name="no-colon" />-->
<!--      &lt;!&ndash; 第二层 &ndash;&gt;-->
<!--      <el-descriptions-item>-->
<!--        <template #label><span style="color: red">优惠劵优惠: </span></template>-->
<!--        {{ fenToYuan(formData.couponPrice!) }} 元-->
<!--      </el-descriptions-item>-->
<!--      <el-descriptions-item>-->
<!--        <template #label><span style="color: red">VIP 优惠: </span></template>-->
<!--        {{ fenToYuan(formData.vipPrice!) }} 元-->
<!--      </el-descriptions-item>-->
<!--      <el-descriptions-item>-->
<!--        <template #label><span style="color: red">活动优惠: </span></template>-->
<!--        {{ fenToYuan(formData.discountPrice!) }} 元-->
<!--      </el-descriptions-item>-->
<!--      <el-descriptions-item>-->
<!--        <template #label><span style="color: red">积分抵扣: </span></template>-->
<!--        {{ fenToYuan(formData.pointPrice!) }} 元-->
<!--      </el-descriptions-item>-->
      <!-- 第三层 -->
      <el-descriptions-item v-for="item in 3" :key="item" label-class-name="no-colon" />
      <el-descriptions-item label="应付金额: ">
        {{ formData.paymentPrice  / 100 }}  {{ formData.paymentCurrency }}
      </el-descriptions-item>
      <el-descriptions-item v-for="item in 3" :key="item" label-class-name="no-colon" />
      <el-descriptions-item label="上游订单号: ">
        {{ formData.cdkChannelInnerOrderId || "--" }}
      </el-descriptions-item>
    </el-descriptions>

    <!-- 物流信息 -->
    <el-descriptions :column="8" title="收货信息" v-if="formData.items">
      <el-descriptions-item width="180px" label="发货方式: ">
        <el-tag v-if="formData.items[0].deliveryMethod === EnumDeliveryMethodType.AUTOMATIC">自动</el-tag>
        <el-tag v-if="formData.items[0].deliveryMethod === EnumDeliveryMethodType.MANUAL">手动</el-tag>
      </el-descriptions-item>
      <el-descriptions-item width="180px" label="充值类型: ">
        <el-tag v-if="formData.items[0].rechargeMethod === EnumRechargeMethodType.CARD_CODE">卡密</el-tag>
        <el-tag v-else-if="formData.items[0].rechargeMethod === EnumRechargeMethodType.DIRECT_RECHARGE">直充</el-tag>
      </el-descriptions-item>
      <el-descriptions-item width="280px" label="接收邮箱: ">
        {{ formData.items[0].receiveEmail || "--"}}
      </el-descriptions-item>
      <!--      <el-descriptions-item label="配送方式: ">-->
<!--        <dict-tag :type="DICT_TYPE.TRADE_DELIVERY_TYPE" :value="formData.deliveryType!" />-->
<!--      </el-descriptions-item>-->
<!--      <el-descriptions-item label="收货人: ">{{ formData.receiverName }}</el-descriptions-item>-->
<!--      <el-descriptions-item label="联系电话: ">{{ formData.receiverMobile }}</el-descriptions-item>-->
<!--      &lt;!&ndash; 快递配送 &ndash;&gt;-->
<!--      <div v-if="formData.deliveryType === DeliveryTypeEnum.EXPRESS.type">-->
<!--        <el-descriptions-item v-if="formData.receiverDetailAddress" label="收货地址: ">-->
<!--          {{ formData.receiverAreaName }} {{ formData.receiverDetailAddress }}-->
<!--          <el-link-->
<!--            v-clipboard:copy="formData.receiverAreaName + ' ' + formData.receiverDetailAddress"-->
<!--            v-clipboard:success="clipboardSuccess"-->
<!--            icon="ep:document-copy"-->
<!--            type="primary"-->
<!--          />-->
<!--        </el-descriptions-item>-->
<!--        <el-descriptions-item v-if="formData.logisticsId" label="物流公司: ">-->
<!--          {{ deliveryExpressList.find((item) => item.id === formData.logisticsId)?.name }}-->
<!--        </el-descriptions-item>-->
<!--        <el-descriptions-item v-if="formData.logisticsId" label="运单号: ">-->
<!--          {{ formData.logisticsNo }}-->
<!--        </el-descriptions-item>-->
<!--        <el-descriptions-item v-if="formatDate.deliveryTime" label="发货时间: ">-->
<!--          {{ formatDate(formData.deliveryTime) }}-->
<!--        </el-descriptions-item>-->
<!--        <el-descriptions-item v-for="item in 2" :key="item" label-class-name="no-colon" />-->
<!--        <el-descriptions-item v-if="expressTrackList.length > 0" label="物流详情: ">-->
<!--          <el-timeline>-->
<!--            <el-timeline-item-->
<!--              v-for="(express, index) in expressTrackList"-->
<!--              :key="index"-->
<!--              :timestamp="formatDate(express.time)"-->
<!--            >-->
<!--              {{ express.content }}-->
<!--            </el-timeline-item>-->
<!--          </el-timeline>-->
<!--        </el-descriptions-item>-->
<!--      </div>-->
<!--      &lt;!&ndash; 自提门店 &ndash;&gt;-->
<!--      <div v-if="formData.deliveryType === DeliveryTypeEnum.PICK_UP.type">-->
<!--        <el-descriptions-item v-if="formData.pickUpStoreId" label="自提门店: ">-->
<!--          {{ pickUpStore?.name }}-->
<!--        </el-descriptions-item>-->
<!--      </div>-->
    </el-descriptions>

    <!-- 订单日志 -->
    <el-descriptions title="订单操作日志">
      <el-descriptions-item labelClassName="no-colon">
        <el-timeline>
          <el-timeline-item
            v-for="(log, index) in formData.logs"
            :key="index"
            :timestamp="formatDate(log.createTime!)"
            placement="top"
          >
            <div class="el-timeline-right-content">
              {{ log.content }}
            </div>
            <template #dot>
              <span
                :style="{ backgroundColor: getUserTypeColor(log.userType!) }"
                class="dot-node-style"
              >
                {{ getDictLabel(DICT_TYPE.USER_TYPE, log.userType)[0] }}
              </span>
            </template>
          </el-timeline-item>
        </el-timeline>
      </el-descriptions-item>
    </el-descriptions>
  </ContentWrap>

  <!-- 各种操作的弹窗 -->
  <OrderDeliveryForm ref="deliveryFormRef" @success="getDetail" />
  <OrderUpdateRemarkForm ref="updateRemarkForm" @success="getDetail" />
  <OrderUpdateAddressForm ref="updateAddressFormRef" @success="getDetail" />
  <OrderUpdatePriceForm ref="updatePriceFormRef" @success="getDetail" />
  <!--填写卡密-->
  <OrderSendCardKeyForm @audit-success="getDetail" :order-info="formData" ref="orderSendCardKeyFormRef" />
  <OrderDrawBackForm @audit-success="getDetail" :order-info="formData" ref="orderDrawBackFormRef" />
  <Dialog class="order-card-key-list-dialog" v-model="isShowCheckCardKeyDialog" title="查看卡密" width="500px">
      <ul >
        <li :key="index" v-for="(cardKey,index) in cardKeyList">
          <table >
            <tr v-if="cardKey.cardNumber">
              <td style="width:40px;text-align:center;" :rowspan="cardKey.tdCount">{{index + 1}}</td>
              <td style="padding-top:16px;">
                <div >
                  <p >卡号：</p>
                  <p >{{cardKey.cardNumber}}</p>
                </div>
              </td>
            </tr>
            <tr v-if="cardKey.code">
              <td >
                <div >
                  <p >密码：</p>
                  <p >{{cardKey.code}}</p>
                </div>
              </td>
            </tr>
            <tr v-if="cardKey.pin">
              <td style="padding-bottom:16px;">
                <div >
                  <p >PIN：</p>
                  <p >{{cardKey.pin}}</p>
                </div>
              </td>
            </tr>
          </table>
        </li>
      </ul>
  </Dialog>
</template>
<script lang="ts" setup>
import * as TradeOrderApi from '@/api/mall/trade/order'
import { fenToYuan } from '@/utils'
import { formatDate } from '@/utils/formatTime'
import { DICT_TYPE, getDictLabel, getDictObj } from '@/utils/dict'
import OrderUpdateRemarkForm from '@/views/mall/trade/order/form/OrderUpdateRemarkForm.vue'
import OrderDeliveryForm from '@/views/mall/trade/order/form/OrderDeliveryForm.vue'
import OrderUpdateAddressForm from '@/views/mall/trade/order/form/OrderUpdateAddressForm.vue'
import OrderUpdatePriceForm from '@/views/mall/trade/order/form/OrderUpdatePriceForm.vue'
import * as DeliveryExpressApi from '@/api/mall/trade/delivery/express'
import { useTagsViewStore } from '@/store/modules/tagsView'
import { DeliveryTypeEnum, TradeOrderStatusEnum } from '@/utils/constants'
import * as DeliveryPickUpStoreApi from '@/api/mall/trade/delivery/pickUpStore'
import { propTypes } from '@/utils/propTypes'
import {EnumDeliveryMethodType, EnumRechargeMethodType} from "@/config/spu";
import OrderSendCardKeyForm from "@/views/mall/trade/order/components/OrderSendCardKeyForm.vue";
import OrderDrawBackForm from "@/views/mall/trade/order/components/OrderDrawBackForm.vue";
import {sendOrderEmail} from "@/api/mall/trade/order";
import {getByOrderNo} from "@/api/digital/cardkeyproduct";
import * as UserApi from "@/api/member/user";
import {ElMessage} from "element-plus";

defineOptions({ name: 'TradeOrderDetail' })

const message = useMessage() // 消息弹窗
const orderSendCardKeyFormRef = ref();
const orderDrawBackFormRef = ref();
const cardKeyList = ref([]);
const isShowCheckCardKeyDialog = ref(false);
const isShowCheckCardKeyBtn = ref(false);
const isLoadingCardKey = ref(false);

/** 获得 userType 颜色 */
const getUserTypeColor = (type: number) => {
  const dict = getDictObj(DICT_TYPE.USER_TYPE, type)
  switch (dict?.colorType) {
    case 'success':
      return '#67C23A'
    case 'info':
      return '#909399'
    case 'warning':
      return '#E6A23C'
    case 'danger':
      return '#F56C6C'
  }
  return '#409EFF'
}

// 订单详情
const formData = ref<TradeOrderApi.OrderVO>({
  logs: []
})

const openAuditForm = ()=> {
  orderSendCardKeyFormRef.value.open();
}

const openDrawBackForm = ()=> {
  orderDrawBackFormRef.value.open();
}

/** 各种操作 */
const updateRemarkForm = ref() // 订单备注表单 Ref
const remark = () => {
  updateRemarkForm.value?.open(formData.value)
}

const blockUser = async ()=> {
  await message.confirm(formData?.value.user?.blocked ? '确认取消拉黑?' : "确认拉黑?");

  UserApi.memberBlaock({
    id: formData?.value.user?.id,
    blocked: !formData?.value.user?.blocked
  }).then(()=>{
    ElMessage.success('操作成功');
    setTimeout(()=> {
      location.reload();
    }, 200);
  }).catch((e:any)=>{
    ElMessage.error(e.message)
  });
}

const sendEmailByHand = ()=> {
  sendOrderEmail(formData.value.id).then(()=> {
    message.success("发送成功");
  });
}

const deliveryFormRef = ref() // 发货表单 Ref
const delivery = () => {
  deliveryFormRef.value?.open(formData.value)
}
const updateAddressFormRef = ref() // 收货地址表单 Ref
const updateAddress = () => {
  updateAddressFormRef.value?.open(formData.value)
}
const updatePriceFormRef = ref() // 订单调价表单 Ref
const updatePrice = () => {
  updatePriceFormRef.value?.open(formData.value)
}

/** 核销 */
const handlePickUp = async () => {
  try {
    // 二次确认
    await message.confirm('确认核销订单吗？')
    // 提交
    await TradeOrderApi.pickUpOrder(formData.value.id!)
    message.success('核销成功')
    // 刷新列表
    await getDetail()
  } catch {}
}

const checkCardKey = ()=> {
  isLoadingCardKey.value = true;
  getByOrderNo({
    orderNo: formData.value.no
  }).then(list=> {
    cardKeyList.value = list.map(e=> {
      let count = 0;

      if(e.cardNumber) {
        count++;
      }
      if(e.code) {
        count++;
      }
      if(e.pin) {
        count++;
      }

      e.tdCount = count;

      return e;
    });

    isShowCheckCardKeyDialog.value = true;
  }).finally(e=> {
    isLoadingCardKey.value = false;
  })
}

/** 获得详情 */
const { params } = useRoute() // 查询参数
const props = defineProps({
  id: propTypes.number.def(undefined), // 订单ID
  showPickUp: propTypes.bool.def(true) // 显示核销按钮
})
const id = (params.id || props.id) as unknown as number
const getDetail = async () => {
  if (id) {
    const res = (await TradeOrderApi.getOrder(id)) as TradeOrderApi.OrderVO
    // 没有表单信息则关闭页面返回
    if (!res) {
      message.error('交易订单不存在')
      close()
    }
    res.logs = res.logs.sort((a,b)=> {
      return a.createTime > b.createTime ? 1 : -1;
    });
    formData.value = res
    isShowCheckCardKeyBtn.value = res.logs?.find(e=> e.operateType === 21);
  }
}

/** 关闭 tag */
const { delView } = useTagsViewStore() // 视图操作
const { push, currentRoute } = useRouter() // 路由
const close = () => {
  delView(unref(currentRoute))
  push({ name: 'TradeOrder' })
}

/** 复制 */
const clipboardSuccess = () => {
  message.success('复制成功')
}

/** 初始化 **/
const deliveryExpressList = ref([]) // 物流公司
const expressTrackList = ref([]) // 物流详情
const pickUpStore = ref({}) // 自提门店
onMounted(async () => {
  await getDetail()
  // 如果配送方式为快递，则查询物流公司
  if (formData.value.deliveryType === DeliveryTypeEnum.EXPRESS.type) {
    deliveryExpressList.value = await DeliveryExpressApi.getSimpleDeliveryExpressList()
    if (form.value.logisticsId) {
      expressTrackList.value = await TradeOrderApi.getExpressTrackList(formData.value.id!)
    }
  } else if (formData.value.deliveryType === DeliveryTypeEnum.PICK_UP.type) {
    pickUpStore.value = await DeliveryPickUpStoreApi.getDeliveryPickUpStore(
      formData.value.pickUpStoreId
    )
  }
  let totalItemNominalValue = 0;
  formData.value.items.forEach(item=> {
    totalItemNominalValue += (item.itemNominalValue) * item.count;
  });
  formData.value.itemNominalValue = totalItemNominalValue;
  console.log(totalItemNominalValue)
})
</script>
<style lang="scss" scoped>
:deep(.el-descriptions) {
  &:not(:nth-child(1)) {
    margin-top: 20px;
  }

  .el-descriptions__title {
    display: flex;
    align-items: center;

    &::before {
      display: inline-block;
      width: 3px;
      height: 20px;
      margin-right: 10px;
      background-color: #409eff;
      content: '';
    }
  }

  .el-descriptions-item__container {
    margin: 0 10px;

    .no-colon {
      margin: 0;

      &::after {
        content: '';
      }
    }
  }
}

// 时间线样式调整
:deep(.el-timeline) {
  margin: 10px 0 0 160px;

  .el-timeline-item__wrapper {
    position: relative;
    top: -20px;

    .el-timeline-item__timestamp {
      position: absolute !important;
      top: 10px;
      left: -150px;
    }
  }

  .el-timeline-right-content {
    display: flex;
    align-items: center;
    min-height: 30px;
    padding: 10px;
    background-color: #f7f8fa;

    &::before {
      position: absolute;
      top: 10px;
      left: 13px; /* 将伪元素水平居中 */
      border-color: transparent #f7f8fa transparent transparent; /* 尖角颜色，左侧朝向 */
      border-style: solid;
      border-width: 8px; /* 调整尖角大小 */
      content: ''; /* 必须设置 content 属性 */
    }
  }

  .dot-node-style {
    position: absolute;
    left: -5px;
    display: flex;
    width: 20px;
    height: 20px;
    font-size: 10px;
    color: #fff;
    border-radius: 50%;
    justify-content: center;
    align-items: center;
  }
}

.order-card-key-list-dialog {
  ul {
    list-style: none;
    padding: 0;
  }

  table {
    border-collapse: collapse;
    width: 100%;

    td {
      border: 1px solid #F1F1F1;
      padding: 9px 0px;
      line-height: 18px;
      font-size: 14px;
      div {
        display: flex;
        p:first-of-type {
          width: 80px;
          text-align: right;
          color: #999;
        }
        p:last-of-type {
          flex: 1;
          word-break: break-all;
          color: #333;
        }
        a {
          width: 60px;
          text-align: right;
          color: #F43E06;
        }
      }
    }
  }
}
</style>
